package com.omg.server.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class DateUtil {

	private static final Log logger = LogFactory.getLog(DateUtil.class);
	public static final String PATTERN_MONTH = "yyyy-MM";
	public static final String PATTERN_MMDD = "yyyy/MM/dd";
	public static final String PATTERN_MMDDHHMM = "yyyy/MM/dd HH:mm";
    public static final String PATTERN = "yyyy-MM-dd";
    public static final String PATTERN_LONG = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_NOSECOND = "yyyy-MM-dd HH:mm";
    public static final String HHMMSSSSS = "HHmmssSSS";
    public static final String HHMMSSSS = "HHmmssSS";
    public static final String HHMM = "HH:mm";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String MMDDHHMM = "MM/dd hh:mm";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYY_MM_DD_HHMM = "yyyy.MM.dd HH:mm";
    public static final String YYYYMMDD_HHMM = "yyyyMMdd HH:mm";
    public static final String YYYYMMDDHH = "yyyyMMddHH";
    public static final String YYYYMMDDHHMMSSssss = "yyyyMMddHHmmssSSSS";
    
    /**
     * 时间转换为指定的格式字符串
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDate(Date date, String pattern){
        if (date == null){
            return "";
        }
        SimpleDateFormat sf = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
        sf.applyPattern(pattern);

        return sf.format(date);

    }

    /**
     * 日期字符串转换成日期，
     * 
     * @param date
     * @return
     */
    public static Date convertToDate(String dateStr){
        if (CommonUtil.isEmpty(dateStr)){
            return null;
        }
        SimpleDateFormat sf = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
        if (dateStr.indexOf(":") == -1){
            sf.applyPattern(PATTERN);
        }
        else{
            sf.applyPattern(PATTERN_LONG);
        }
        Date date = null;
        try{
            date = sf.parse(dateStr);
        }
        catch (ParseException e){
            logger.error("日期格式化错误:" + e.getMessage());
        }
        return date;

    }

    /**
     * 日期字符串转换成指定格式日期，
     * 
     * @param date
     * @param pattern
     * @return
     */
    public static Date convertToDate(String dateStr, String pattern){
        if (CommonUtil.isEmpty(dateStr)){
            return null;
        }
        SimpleDateFormat sf = new SimpleDateFormat(pattern, Locale.CHINESE); 
        Date date = null;
        try{
            date = sf.parse(dateStr);
        }
        catch (ParseException e){
            logger.error("日期格式化错误:" + e.getMessage());
            e.printStackTrace();
        }
        return date;

    }
    
    public static void main(String[] args){
    	for (int i=0; i < 1000; i++){
    	  String data = "2011-5-17 1:36";
    	  System.out.println(convertToDate(data, PATTERN_NOSECOND));
    	}
    }
    
}
